#include "wuBaseManager.h"

wuBaseManager::wuBaseManager()
{

}

wuBaseManager::~wuBaseManager()
{
}

void wuBaseManager::setup(int _vw, int _vh){
    vw = _vw;
    vh = _vh;

	color.allocate(vw, vh);
	bgColor.allocate(vw, vh);
	gray.allocate(vw, vh);
	bgGrayImg.allocate(vw, vh);
	grayDiff.allocate(vw, vh);

	minBlobSize = 20;
	maxBlobSize = 3*(vw*vh)/2;
	maxBlobs = 80;
	threshold = 11;

	bGrayDiff_defined = false;
    bBg_captured = false;
	bBypass = false;
	vBlur = 5;
	status = STAT_NONE;
	vPriority = 5;
	bScrollable = true;
    bSelectable = true;

    selection.x = 0;
    selection.y = 0;
    selection.width = vw;
    selection.height = vh;
}

void wuBaseManager::setMaxBlobs(int _maxBlobs)
{
	maxBlobs = _maxBlobs;
}

void wuBaseManager::setBlur(int _vBlur)
{
	vBlur = _vBlur;
}

void wuBaseManager::setMinBlobSize(int _minBlobSize)
{
	minBlobSize = _minBlobSize;
}

void wuBaseManager::setMaxBlobSize(int _maxBlobSize)
{
	maxBlobSize = _maxBlobSize;
}

void wuBaseManager::setThreshold(int _thres)
{
	threshold = _thres;
}

void wuBaseManager::setGrayDiff(unsigned char * _pixels)
{
	grayDiff.setFromPixels(_pixels, vw, vh);
	bGrayDiff_defined = true;
}

void wuBaseManager::setBg(ofImage *bgImg)
{
    bgColor.setFromPixels(bgImg->getPixels(), vw,vh);
	bBg_captured = true;
}

void wuBaseManager::capture(ofImage *currImg, ofImage *bgImg, int x_offset)
{
    if(!bBypass)
    {
        color.setFromPixels(currImg->getPixels(), vw,vh);
        gray = color;
        if(!bGrayDiff_defined)
        {
            if(!bBg_captured)
                bgColor.setFromPixels(bgImg->getPixels(), vw,vh);
            bgGrayImg = bgColor;

            // take the abs value of the difference between background and incoming and then threshold:
            grayDiff.absDiff(gray, bgGrayImg);
            grayDiff.threshold(threshold);
        }

        grayDiff.blur(vBlur);
        contour.blobs.clear();
        contour.findContours(grayDiff, minBlobSize, maxBlobSize, maxBlobs, false);	// find holes

        for (int i = 0; i < contour.nBlobs; i++)
        {
            ofRectangle rect;

            rect.x = contour.blobs[i].boundingRect.x;
            rect.y = contour.blobs[i].boundingRect.y;
            rect.width = contour.blobs[i].boundingRect.width;
            rect.height = contour.blobs[i].boundingRect.height;

            wuImagePlus _skin;
            _skin.crop(color.getPixels(), grayDiff.getPixels(), vw, vh, rect);

            if(!bSelectable || ((rect.x > selection.x && rect.y > selection.y && (rect.x + rect.width < selection.x + selection.width) && (rect.y + rect.height < selection.y + selection.height)) ||
               (selection.height==vh && selection.width==vw) || (selection.height==0 && selection.width==0)))
            {
                vector <ofPoint> pts_off;
                for(int j=0; j< contour.blobs[i].pts.size(); j++)
                {
                    ofPoint p_tmp;
                    p_tmp.x = contour.blobs[i].pts[j].x + (x_offset*vPriority);
                    p_tmp.y = contour.blobs[i].pts[j].y;
                    pts_off.push_back(p_tmp);
                }
                rect.x = rect.x + (x_offset*vPriority);
                addBitxo(&_skin, rect, &(pts_off));
           }
 //           else
 //               addBitxo(&_skin, rect, &(contour.blobs[i].pts));
        }
    }
}

void wuBaseManager::addBitxo(ofImage *_skin, ofRectangle rect, vector <ofPoint> * _pts)
{
}

bool wuBaseManager::action(ofImage *currImg, ofImage *bgImg, int action, int x_offset)
{
    bool bCambiazo = false;

    x_scroll = x_offset*vPriority;

    if (action==ACT_CLEAR)
    {
        clear();
        status = STAT_NONE;
    }
    if (action==ACT_CAP_SKIN)
    {
        capture(currImg, bgImg, x_offset);
        status = STAT_SKIN;
        bCambiazo = true;
    }

    return bCambiazo;
}

void wuBaseManager::update()
{
}
void wuBaseManager::clear()
{
}
void wuBaseManager::draw(bool pinta_bg, int offX)
{
}

void wuBaseManager::drawInvisible(bool pinta_bg, int offX)
{
}

void wuBaseManager::bypass(bool _bBypass){
   	bBypass = _bBypass;
}

void wuBaseManager::touch(int _x,int _y, int forceX, int forceY)
{
}
bool wuBaseManager::drag(int _x,int _y)
{
    return false;
}

void wuBaseManager::dragOff()
{
}

int wuBaseManager::getStatus()
{
   return status;
}

void wuBaseManager::setPriority(int _prior)
{
   vPriority = _prior;
}

int wuBaseManager::getPriority()
{
   return vPriority;
}

void wuBaseManager::setScrollable(bool _bScrollable){
   	bScrollable = _bScrollable;
}

void wuBaseManager::setScrollOffset(int _scrollOffset)
{
    x_scroll = _scrollOffset*vPriority;
}

void wuBaseManager::setSelection(ofRectangle _selection)
{
    selection = _selection;
}

void wuBaseManager::drawSelected(bool pinta_bg, int offX)
{
}

void wuBaseManager::setSelectable(bool _bSelectable){
   	bSelectable = _bSelectable;
}
